<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入 
* G6 (https://github.com/antvis/G6)
*********************************************************************-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <title data-i18n="resources.title_l7_custom_water_level_chart"></title>
    <script type="text/javascript" include="jquery" src="../js/include-web.js"></script>
    <style>
      body {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100vh;
        overflow: hidden;
      }
      #map {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>

  <body>
    <div id="map"></div>
    <script type="text/javascript" include="mapbox-gl-enhance,L7" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
    <script type="text/javascript">
      var host = window.isLocal ? window.server : 'https://iserver.supermap.io';
      var attribution =
        "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
        " with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
        " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";

      var map = new mapboxgl.Map({
        container: 'map',
        style: {
          version: 8,
          sources: {
            'raster-tiles': {
              attribution: attribution,
              type: 'raster',
              tiles: [
                host +
                  '/iserver/services/map-china/rest/maps/China/zxyTileImage.png?prjCoordSys=' +
                  encodeURIComponent('{"epsgCode":3857}') +
                  '&z={z}&x={x}&y={y}'
              ],
              tileSize: 256
            }
          },
          layers: [
            {
              id: 'simple-tiles',
              type: 'raster',
              source: 'raster-tiles',
              minzoom: 0,
              maxzoom: 22
            }
          ]
        },
        center: [115, 31],
        zoom: 5.0
      });
      let x = 0;
      function draw(option) {
        const { size, ctx, mapService } = option;
        const [width, height] = size;

        const radius = 30,
          rectWidth = radius * 2;
        const rectHeight = rectWidth;

        ctx.clearRect(0, 0, width, height);

        const points = [
          {
            lng: 108.544921875,
            lat: 30.977609093348686,
            level: 85,
            color: 'rgba(	220,20,60, 0.6)'
          },
          {
            lng: 110.654296875,
            lat: 31.090574094954192,
            level: 75,
            color: 'rgba(	255,140,0, 0.6)'
          },
          {
            lng: 112.5,
            lat: 29.80251790576445,
            level: 65,
            color: 'rgba(255,165,0, 0.6)'
          },
          {
            lng: 114.78515624999999,
            lat: 30.64867367928756,
            level: 40,
            color: 'rgba(30,144,255, 0.6)'
          },
          {
            lng: 116.49902343749999,
            lat: 29.84064389983441,
            level: 50,
            color: 'rgba(30,144,255, 0.6)'
          },
          {
            lng: 118.21289062499999,
            lat: 31.16580958786196,
            level: 20,
            color: 'rgba(	127,255,0, 0.6)'
          },
          {
            lng: 119.091796875,
            lat: 32.509761735919426,
            level: 50,
            color: 'rgba(30,144,255, 0.6)'
          },
          {
            lng: 121.0693359374999,
            lat: 31.80289258670676,
            level: 45,
            color: 'rgba(30,144,255, 0.6)'
          }
        ];
        ctx.fillStyle = 'rgb(35,75,225)';
        ctx.font = 'normal small-caps bold 14px arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        points.map((point) => {
          const pixelCenter = mapService.lngLatToContainer([point.lng, point.lat]);
          pixelCenter.x *= window.devicePixelRatio;
          pixelCenter.y *= window.devicePixelRatio;
          const rectStartX = pixelCenter.x - radius;
          const rectStartY = pixelCenter.y - radius;

          ctx.save();

          ctx.fillText(point.level + '%', pixelCenter.x, pixelCenter.y);

          ctx.beginPath();
          ctx.arc(pixelCenter.x, pixelCenter.y, radius, 0, Math.PI * 2);
          ctx.fillStyle = 'rgba(135,206,250,0.2)';
          ctx.closePath();
          ctx.fill();
          ctx.clip();

          ctx.beginPath();
          ctx.fillStyle = point.color;
          ctx.moveTo(rectStartX, pixelCenter.y);

          const waterheight = rectStartY + ((100 - point.level) / 100) * rectHeight;
          for (let i = 0; i <= rectWidth; i += 10) {
            ctx.lineTo(rectStartX + i, waterheight + Math.sin(Math.PI * 2 * (i / rectWidth) + x) * 3 + 1);
          }

          ctx.lineTo(pixelCenter.x + radius, pixelCenter.y + radius);
          ctx.lineTo(rectStartX, pixelCenter.y + radius);
          ctx.lineTo(rectStartX, pixelCenter.y);
          ctx.closePath();

          ctx.fill();

          ctx.restore();
          return '';
        });
        return '';
      }

      map.on('load', function () {
        var layer, l7Layer, l7Scene;
        map.getL7Scene().then((scene) => {
          l7Scene = scene;
          layer = new mapboxgl.supermap.L7Layer({ type: 'CanvasLayer' });
          l7Layer = layer.getL7Layer();
          l7Layer
            .style({
              update: 'always',
              drawingOnCanvas: draw
            })
            .animate({
              enable: true
            });
          map.addLayer(layer);
        });
        setInterval(() => {
          x += 0.1;
          l7Layer.style({
            drawingOnCanvas: draw
          });
          l7Layer.reRender();
          return '';
        }, 30);
      });
    </script>
  </body>
</html>
